home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Disc to the Future 2
/
Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin
/
MAC
/
MACSHELL
/
MS1
/
SHELL_SO
/
STDIOPRO.C
< prev
next >
Wrap
Text File
|
1992-12-02
|
10KB
|
393 lines
/*
* MacShell Source File
*
* Copyright (c) 1989, 1990, 1991, 1992 Suick Bay Technologies. All rights reserved.
*
*
* RESTRICTIONS ON MacShell program and source code.
*
* Ñ╩MacShell¬ is a product of Suick Bay Technologies and is provided for
* restricted use by the owner of the CDROM "Disk to the future II".
*
* Ñ╩No permission is granted for any commercial use without the written
* consent of the Suick Bay Technologies.
*
* Ñ╩No permission is granted for any redistribution of any kind use without
* the written consent of the Suick Bay Technologies.
*
* Ñ╩Permission is granted to use this for any personal noncommercial use.
*
* Ñ╩You may not distribute source or executable code at all, nor may you
* distribute it with or within a commercial product without the written
* consent of the Suick Bay Technologies. Please send modifications to
* the author for inclusion in updates to the program. Thanks.
*
*
* MacShell¬ IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
* WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
* PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
*
* SUICK BAY TECHNOLOGIES SHALL HAVE NO LIABILITY WITH RESPECT TO THE
* INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY MACSHELL
* OR ANY PART THEREOF.
*
* In no event will Suick Bay Technologies be liable for any lost revenue
* or profits or other special, indirect and consequential damages, even if
* Suick Bay Technologies has been advised of the possibility of such damages.
*
* Suick Bay Technologies can be reached at:
*
* 8768 Cottonwood lane
* Maple Grove, MN 55369
* Voice: (612) 425-7025
* AppleLink: D5233
*
*
* No parts of this software may be reproduced or stored in a
* retrieval system or transmitted in any form, or any means,
* electronic, mechanical, photocopying, recording or otherwise,
* without the prior written permission of Suick Bay Technologies.
*
* Spread the word and not the disk.
*
* SPK 012290 : Initial
*
*
* Psuedo process function file
*
* Ñ╩Standard Input Function
*
* Ñ╩Standard Output Function
*
* Ñ╩Standard Error Output Function
*/
#include <FileMgr.h>
#include "System.h"
#include "Struct.h"
#include "proc.h"
#include "Shell.h"
#include "path.h"
#include "EditU.h"
#define BUFSIZE 256
/*******************************************************************/
Boolean StdInProc( int ProcToken, WHandle ShellWh, int ProcID,
char *string )
{
int pwdVRefNum = 0, err;
pathType pt;
char readBuf[ BUFSIZE ], *cp;
long parDirID = 0L, pwdDirID = 0L, count = BUFSIZE - 1;
ShellWindRec **MyShell;
MyShell = (ShellWindRec **) (**ShellWh).thing;
switch( ProcToken )
{
case PROC_INIT :
/* if a string, then open it as a file */
(**MyShell).Proc[ ProcID ].fileRefNum =
OpenFileDirect( string, 'TEXT', fsRdPerm );
ResetShellPWD( ShellWh );
if( (**MyShell).Proc[ ProcID ].fileRefNum )
{
#ifdef PROCDEBUG
printf( "Openned STDIN file %ps with err %d\n",
string, err );
#endif
return( TRUE );
}
return( FALSE );
case PROC_TERM :
case PROC_BREAK :
/* if a file open, close it */
if( (**MyShell).Proc[ ProcID ].fileRefNum )
{
FSClose( (**MyShell).Proc[ ProcID ].fileRefNum );
(**MyShell).Proc[ ProcID ].fileRefNum = 0;
}
SendOutToken( ShellWh, ProcID, PROC_BREAK );
/* Turn ourself off */
if( ProcID != STDIN_PROCID )
(**MyShell).Proc[ ProcID ].ProcActive = FALSE;
else
(**MyShell).Proc[ ProcID ].StdOutProcID = CL_PROCID;
#ifdef PROCDEBUG
printf( "Reset STDIN Proc to Shell\n" );
#endif
break;
case PROC_STDIN :
/* if there is a file open, read a line and send it, return TRUE */
if( (**MyShell).Proc[ ProcID ].fileRefNum )
{
err = FSRead( (**MyShell).Proc[ ProcID ].fileRefNum, &count, readBuf );
readBuf[ count ] = '\0';
if( err == eofErr ) /* if we are at the end of file */
{
StdOut( ShellWh, ProcID, readBuf );
/* send a break to StdOut */
SendOutToken( ShellWh, ProcID, PROC_BREAK );
/* if a file open, close it */
FSClose( (**MyShell).Proc[ ProcID ].fileRefNum );
(**MyShell).Proc[ ProcID ].fileRefNum = 0;
/* Turn ourself off */
if( ProcID != STDIN_PROCID )
(**MyShell).Proc[ ProcID ].ProcActive = FALSE;
}
else if( err )
{
/* send a break to StdOut */
SendOutToken( ShellWh, ProcID, PROC_BREAK );
/* if a file open, close it */
FSClose( (**MyShell).Proc[ ProcID ].fileRefNum );
(**MyShell).Proc[ ProcID ].fileRefNum = 0;
/* Turn ourself off */
if( ProcID != STDIN_PROCID )
(**MyShell).Proc[ ProcID ].ProcActive = FALSE;
}
else
StdOut( ShellWh, ProcID, readBuf );
}
else if( string ) /* write it out to the stream */
StdOut( ShellWh, ProcID, string );
else if( (**ShellWh).entryCnt == 0 )
/* Look in the input PE for a character string */
{
char command[ CMDLINESIZE ], *sp, *cp, **sh, *base;
int32 peLen, cmdLen = 0L, prompt;
PEHandle hPE;
Boolean found = FALSE;
hPE = (**MyShell).ihPE;
sh = (**hPE).hText;
HLock( sh );
sp = *sh;
cp = command;
peLen = (**MyShell).promptBase; /* move past prompt */
while( peLen )
{
sp++;
peLen--;
}
base = sp;
peLen = (**hPE).peLength - (**MyShell).promptBase;
if( (*sp == '\r') && (peLen > 0)) /* NULL command */
{
found = TRUE;
cmdLen++;
}
else
{
PERemoveGap( hPE );
while( (peLen > 0) && (cmdLen < (int32)CMDLINESIZE) )
{
if( *sp == '\r')
{
cmdLen++;
found = TRUE;
break;
}
*cp++ = *sp++;
cmdLen++;
peLen--;
}
}
if( found ) /* something to do */
{
register WHandler **wh, **wh2;
register WindowPtr w;
int32 saveBase = (**MyShell).promptBase;
*cp++ = '\n';
*cp = '\0'; /* complete command */
/* Preset undo string to the command */
PESetSelect( (**MyShell).promptBase,
(**MyShell).promptBase + cmdLen-1L, hPE );
PresetUndo( uCLEAR, hPE );
PESetSelect( 0L, (**MyShell).promptBase + cmdLen, hPE );
PEDelete( hPE ); /* delete the prompt and the command */
(**MyShell).promptBase = 0L;
StdOut( ShellWh, ProcID, command ); /* do the string */
(**MyShell).promptBase = saveBase;
DoShellPrompt( ShellWh ); /* indicate command ready */
PESetSelect( (**MyShell).promptBase,
(**MyShell).promptBase, hPE );
SetPeditEditStatus( hPE, ShellWh );
SetUndo( hPE );
prompt = (**MyShell).prompt;
if( prompt != (**MyShell).prompt )
{
PESetSelect( 0L, (**MyShell).promptBase, hPE );
PEDelete( hPE );
DoShellPrompt( ShellWh );
}
AdjustInput( ShellWh, TRUE );
PESetSelect( (**hPE).peLength, (**hPE).peLength, hPE );
}
HUnlock( sh );
}
break;
default :
break;
}
return( TRUE );
}
/*******************************************************************/
Boolean StdOutProc( int ProcToken, WHandle ShellWh, int ProcID,
char *string )
{
int vRefNum = 0, err;
long count;
ShellWindRec **MyShell;
MyShell = (ShellWindRec **) (**ShellWh).thing;
switch( ProcToken )
{
case PROC_INIT :
/* if a string, then open it as a file */
if( (**MyShell).StdOutAppend == FALSE )
{
if( err = CreateFile( string, 'TEXT' ) )
return( FALSE );
ResetShellPWD( ShellWh );
}
(**MyShell).Proc[ ProcID ].fileRefNum =
OpenFileDirect( string, 'TEXT', fsWrPerm );
ResetShellPWD( ShellWh );
if( (**MyShell).Proc[ ProcID ].fileRefNum )
{
#ifdef PROCDEBUG
printf( "Openned STDOUT file %ps with err %d\n",
string, err );
#endif
if( (**MyShell).StdOutAppend )
SetFPos( (**MyShell).Proc[ ProcID ].fileRefNum, fsFromLEOF, 0L );
return( TRUE );
}
else /* file did not exist create it and continue */
{
if( err = CreateFile( string, 'TEXT' ) )
{
FileError( err );
return( FALSE );
}
(**MyShell).Proc[ ProcID ].fileRefNum =
OpenFileDirect( string, 'TEXT', fsWrPerm );
ResetShellPWD( ShellWh );
if( (**MyShell).Proc[ ProcID ].fileRefNum )
return( TRUE );
}
return( FALSE );
case PROC_TERM :
case PROC_BREAK :
/* if a file open, close it */
if( (**MyShell).Proc[ ProcID ].fileRefNum )
{
FSClose( (**MyShell).Proc[ ProcID ].fileRefNum );
(**MyShell).Proc[ ProcID ].fileRefNum = 0;
}
/* Tell the shell that we're done */
ShellOutProc( PROC_BREAK, ShellWh, ProcID, NULL );
/* Turn ourself off */
if( ProcID != STDOUT_PROCID )
(**MyShell).Proc[ ProcID ].ProcActive = FALSE;
break;
case PROC_STDIN :
/* is there a file open, write the string, return TRUE */
if( (**MyShell).Proc[ ProcID ].fileRefNum && string && *string )
{
count = (long) strlen( string );
err = FSWrite( (**MyShell).Proc[ ProcID ].fileRefNum,
&count, string );
if( err )
{
/* send a break to StdOut */
SendOutToken( ShellWh, ProcID, PROC_BREAK );
/* if a file open, close it */
FSClose( (**MyShell).Proc[ ProcID ].fileRefNum );
(**MyShell).Proc[ ProcID ].fileRefNum = 0;
/* Turn ourself off */
if( ProcID != STDOUT_PROCID )
(**MyShell).Proc[ ProcID ].ProcActive = FALSE;
}
}
else if( string ) /* write it out to the shell */
ShellOutProc( PROC_STDIN, ShellWh, ProcID, string );
break;
}
}
/*******************************************************************/
Boolean StdErrProc( int ProcToken, WHandle ShellWh, int ProcID, char *string )
{
switch( ProcToken )
{
case PROC_INIT : /* redirect err output to a file */
break;
case PROC_TERM :
case PROC_BREAK :
break;
case PROC_STDIN :
ShellOutProc( PROC_STDIN, ShellWh, ProcID, string );
break;
}
}